<?php

namespace App\Observers\Asset;

use App\Contracts\CacheKey;
use App\Models\AssetAffiliation;
use App\Models\AssetBrand;
use App\Models\AssetCategory;
use App\Models\AssetKeeper;
use App\Models\AssetLocation;
use App\Models\AssetStatus;
use App\Models\AssetVendor;
use Cache;
use Illuminate\Database\Eloquent\Model;

class AssetPivotObserver
{

    protected function _clearCache($key)
    {
        Cache::tags([CacheKey::TAG_META])->forget($key);
    }

    public function created(Model $assetPivot)
    {

        $className = get_class($assetPivot);
        switch ($className) {
            case AssetAffiliation::class :
                $this->_clearCache(CacheKey::$KEY_ASSET_AFFILIATIONS);
                break;
            case AssetBrand::class:
                $this->_clearCache(CacheKey::$KEY_ASSET_BRANDS);
                break;
            case AssetKeeper::class:
                $this->_clearCache(CacheKey::$KEY_ASSET_KEEPERS);
                break;
            case AssetVendor::class:
                $this->_clearCache(CacheKey::$KEY_ASSET_VENDORS);
                break;
            case AssetLocation::class:
                $this->_clearCache(CacheKey::$KEY_ASSET_LOCATIONS);
                break;
            case AssetCategory::class:
                $this->_clearCache(CacheKey::$KEY_ASSET_CATEGORIES);
                break;
            case AssetStatus::class:
                $this->_clearCache(CacheKey::$KEY_ASSET_STATUSES);
                break;
        }
    }

    public function updated(Model $assetPivot)
    {
        $this->created($assetPivot);
    }
}
